/*
 * @Author: Wangjun
 * @Date: 2023-08-14 10:40:47
 * @LastEditTime: 2023-12-11 21:08:58
 * @LastEditors: wangjun haodreams@163.com
 * @Description:
 * @FilePath: \xr_node_calc\noderdb\op_ai.go
 * hnxr
 */
package noderdb

import (
	"errors"

	"gitee.com/haodreams/op_node/thirdparty"
	"gitee.com/hnxr_1/lib/device"
	"gitee.com/hnxr_1/nodex"
)

func init() {
	nodex.Reister(new(AI))
}

type AI struct {
	nodex.Node
	item *thirdparty.Item
	Y    nodex.Double `remark:"输出Y,,out"`
	Tag  string       //标签
	Kind string       //类别
}

func (m *AI) GetModelID() int {
	return nodex.ModIn
}

func (m *AI) GetName() string {
	return "AI"
}

func (m *AI) Init(mem *nodex.Memory) (err error) {
	extends := m.GetExtends()
	parent := m.GetParent()
	atag := extends["tag"]
	tag, _ := atag.(string)
	// if ok {
	// 	m.Tag = tag
	// 	m.item = GetItem(parent.Category, parent.RID, tag)
	// }
	acategory := extends["category"]
	category, _ := acategory.(string)
	if tag != "" {
		m.Tag = tag
		m.Kind = category
		m.item = dataitem.GetMemItem(category, parent.RID, tag)
	}

	return
}

/**
 * @description: 节点信息
 * @return {*}
 */
func (m *AI) RegData() []byte {
	return nodex.MakeRegData(m, "内存模拟量输入", m.GetName(), "")
}

func (m *AI) Calc() (err error) {
	if m.item == nil {
		m.Bad = true
		m.Err = errors.New(m.Tag + ":测点不存在")
		if m.EnQ {
			return m.Err
		}
		return
	}
	if m.item.Q == device.QNull {
		m.Err = errors.New(m.Tag + ":测点不存在")
		m.Bad = true
		if m.EnQ {
			return m.Err
		}
		return
	}
	m.Y.Value = m.item.V
	m.Bad = false
	return
}
